package com.design.movieticket.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.design.movieticket.common.base.constant.Result;
import com.design.movieticket.common.base.constant.ResultStatus;
import com.design.movieticket.common.dto.XPage;
import com.design.movieticket.entity.User;
import com.design.movieticket.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/userInfo")
@Api(tags = "用户模块")
public class UserInfoController {
    @Resource
    private UserService userService;

    /** 分页查询 */
    @ApiOperation("用户信息分页展示")
    @GetMapping("/list/page")
    public Result findUserInfoListByPage(User user, XPage<Void> page) {
        IPage<User> userInfoList = userService.page(XPage.toIPage(page), userService.buildQuery(user));
        return new Result(userInfoList,ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 新增或更新用户数据 */
    @ApiOperation("新增或更新用户数据")
    @PostMapping("/addOrUpdate")
    public Result addOrEditUserInfo(@RequestBody User user) {
        userService.addOrUpdateUserInfo(user);
        return new Result(ResultStatus.SAVE_SUCCESS.getMsg(),ResultStatus.SAVE_SUCCESS.getCode());
    }

    /** 获取用户信息 */
    @ApiOperation("获取用户信息详情")
    @GetMapping("/getUserInfoDetail/{id}")
    public Result getUserInfoDetail(@PathVariable long id){
        return new Result(userService.getUserInfoDetail(id),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 删除用户信息 */
    @ApiOperation("删除用户信息")
    @PostMapping("/delUserInfoById/{id}")
    public Result delUserInfoById(@PathVariable long id){
        userService.delUserInfoById(id);
        return new Result(ResultStatus.DELETE_SUCCESS.getMsg(),ResultStatus.DELETE_SUCCESS.getCode());
    }




    /** 小程序获取用户信息 */
    @GetMapping("/getUserInfoByUsername")
    public Result getUserInfoByUsername(String username){
        System.out.println("触发");
        return new Result(userService.getUserInfoByUsername(username),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }
}
